package com.hctg.forced2sleep.dialog;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.actor.myandroidframework.dialog.ViewBindingDialog;
import com.blankj.utilcode.util.SizeUtils;
import com.hctg.forced2sleep.databinding.DialogOptionsBinding;

import java.util.List;

/**
 * description: 滑动选择
 * company    :
 *
 * @author : ldf
 * date       : 2025/1/8 on 16
 * @version 1.0
 */
public class OptionsDialog extends ViewBindingDialog<DialogOptionsBinding> {

    private String title = "请选择";
    private final List<?> items;
    private final int selectedPos;
    private final OnItemSelectedListener listener;

    public OptionsDialog(@NonNull Context context, @Nullable String title, @NonNull List<?> items,
                         int selectedPos, @NonNull OnItemSelectedListener listener) {
        super(context);
        setWidthPercent(0.888888f, SizeUtils.dp2px(308f));
        if (title != null) this.title = title;
        this.items = items;
        this.selectedPos = selectedPos;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.tvCancel.setOnClickListener(v -> dismiss());
        viewBinding.tvConfirm.setOnClickListener(v -> {
            int pos = viewBinding.npItems.getValue();
            dismiss();
            if (listener != null) listener.OnItemSelected(pos);
        });
        viewBinding.tvTitle.setText(title);
        if (items != null) {
            String[] values = new String[items.size()];
            for (int i = 0; i < items.size(); i++) {
                Object item = items.get(i);
                values[i] = String.valueOf(item);
            }
            viewBinding.npItems.setDisplayedValues(values);
            viewBinding.npItems.setValue(selectedPos);
        }
    }

    public interface OnItemSelectedListener {
        void OnItemSelected(int position);
    }
}
